import request from "@/util/request";
import { adaptPositionList, adaptPersonalPositionDetail } from "@/util/positionAdapter";
//---职位管理模块---

/**
 * 修改岗位信息
 * @param {Object} data - 岗位信息
 * @param {number} data.positionId - 岗位ID
 * @param {string} data.positionName - 岗位名称
 * @param {string} data.positionDetail - 岗位详情
 * @param {string} data.positionRequire - 岗位要求
 * @param {number} data.LowsalaryMonth - 最低月薪
 * @param {number} data.HighsalaryMonth - 最高月薪
 * @param {string} data.positionType -岗位类型
 * @param {string} data.education -学历要求
 * @param {string} data.endAt - 截止日期（格式：YYYY-MM-DD）
 * @returns {Promise<Object>} - 返回修改结果
 */
export const updateUserPosition = (data) => {
  return request.post('/recruiter/position/updateUserPosition', data);
};

/**
 * 重新提交发布的岗位
 * @param {Object} data - 岗位信息
 * @param {string} data.code - 岗位唯一标识
 * @param {string} data.positionName - 岗位名称
 * @param {string} data.positionDetail - 岗位详情
 * @param {string} data.positionRequire - 岗位要求
 * @param {number} data.LowsalaryMonth - 最低月薪
 * @param {number} data.HighsalaryMonth - 最高月薪
 * @param {string} data.positionType -岗位类型
 * @param {string} data.education -学历要求
 * @param {string} data.endAt - 截止日期（格式：YYYY-MM-DD）
 * @returns {Promise<Object>} - 返回提交结果，包含审核状态
 */
export const resubmitPublishPosition = (data) => {
  return request.post('/recruiter/position/resubmitPublishPosition', data);
};

/**
 * 发布岗位
 * @param {Object} data - 岗位信息
 * @param {string} data.positionName - 岗位名称
 * @param {string} data.positionDetail - 岗位详情
 * @param {string} data.positionRequire - 岗位要求
 * @param {number} data.LowsalaryMonth - 最低月薪
 * @param {number} data.HighsalaryMonth - 最高月薪
 * @param {string} data.positionType -岗位类型
 * @param {string} data.education -学历要求
 * @returns {Promise<Object>} - 返回发布结果，包含岗位唯一标识和审核状态
 */
export const publishPosition = (data) => {
  return request.post('/recruiter/position/publishPosition', data);
};

/**
 * 公司审核发布岗位通过
 * @param {string} code - 发布岗位的唯一标识
 * @returns {Promise<Object>} - 返回审核结果
 */
export const companyVerifyPublishPosition = (code) => {
  return request.post('/recruiter/position/companyVerifyPublishPosition', null, { params: { code } });
};

/**
 * 公司审核发布岗位不通过
 * @param {string} code - 发布岗位的唯一标识
 * @param {string} message - 错误反馈信息
 * @returns {Promise<Object>} - 返回审核结果
 */
export const companyVerifyPublishPositionFailed = (code, message) => {
  return request.post('/recruiter/position/companyVerifyPublishPositionFailed', null, { params: { code, message } });
};

/**
 * 公司审核岗位变更通过
 * @param {number} positionId - 职位ID
 * @returns {Promise<Object>} - 返回审核结果
 */
export const companyVerifyChangePosition = (positionId) => {
  return request.post('/recruiter/position/companyVerifyChangePosition', null, { params: { positionId } });
};

/**
 * 公司审核岗位变更不通过
 * @param {number} positionId - 职位ID
 * @returns {Promise<Object>} - 返回审核结果
 */
export const companyVerifyChangePositionFailed = (positionId, failMessage) => {
  return request.post('/recruiter/position/companyVerifyChangePositionFailed', null, { params: { positionId, failMessage } });
};

/**
 * 跨服务-变更岗位
 * @param {Object} data - 岗位信息
 * @param {number} data.positionId - 岗位ID
 * @param {number} data.userId - 用户ID
 * @param {string} data.positionName - 岗位名称
 * @param {string} data.positionDetail - 岗位详情
 * @param {string} data.positionRequire - 岗位要求
 * @param {number} data.salaryMonth - 月薪
 * @param {string} data.endAt - 截止日期（格式：YYYY-MM-DD）
 * @returns {Promise<Object>} - 返回变更结果
 */
export const changePositionInformation = (data) => {
  return request.post('/recruiter/position/changePositionInformation', data);
};

/**
 * 返回个人发布待审核的岗位
 * @param {Object} params - 查询参数（可选）
 * @param {number} [params.pageNum=1] - 页码
 * @param {number} [params.pageSize=10] - 每页数量
 * @param {string} [params.keyword] - 关键词搜索
 * @returns {Promise<Object>} - 返回岗位列表数据
 */
export async function getUserPublishPositions() {
  const res = await request.get("/recruiter/position/getUserPublishPositions");
  return {
    ...res,
    data: adaptPositionList("getUserPublishPositions", res.data.data || [])
  };
};

// 个人发布成功岗位
export async function getUserPositions() {
  const res = await request.get("/recruiter/position/getUserPositions");
  return {
    ...res,
    data: adaptPositionList("getUserPositions", res.data.data || [])
  };
}


// 公司发布待审核岗位
export async function getCompanyPublishPositions() {
  const res = await request.get("/recruiter/position/getCompanyPublishPositions");
  return {
    ...res,
    data: adaptPositionList("getCompanyPublishPositions", res.data.data || [])
  };
}

// 公司发布成功岗位
export async function getCompanyPositions() {
  const res = await request.get("/recruiter/position/getCompanyPositions");
  return {
    ...res,
    data: adaptPositionList("getCompanyPositions", res.data.data || [])
  };
}

/**
 * 删除岗位
 * @param {number} positionId - 职位ID
 * @returns {Promise<Object>} - 返回删除结果
 */
export const deletePosition = (positionId) => {
  return request.delete('/recruiter/position/deletePosition', { params: { positionId } });
};

/**
 * 取消变更岗位信息
 * @param {number} positionId - 职位ID
 * @returns {Promise<Object>} - 返回取消结果
 */
export const cancelUpdateUserPosition = (positionId) => {
  return request.delete('/recruiter/position/cancelUpdateUserPosition', { params: { positionId } });
};

/**
 * 取消发布岗位
 * @param {string} code - 发布岗位的唯一标识
 * @returns {Promise<Object>} - 返回取消结果
 */
export const cancelPublishPosition = (code) => {
  return request.delete('/recruiter/position/cancelPublishPosition', { params: { code } });
};

//获取职位行业列表
export const getAllPositionIndustries = () => {
  return request.get('/common/getAllPositionIndustries')
}

//类别列表
export const getPositionCategoriesByIndustryId = (id) => {
  return request.get('/common/getPositionCategoriesByIndustryId', { params: { id } })
}

//岗位名称
export const getPositionNamesByCategoryId = (id) => {
  return request.get('/common/getPositionNamesByCategoryId', { params: { id } })
}

//根据id返回待审核岗位
export async function getUserPublishPositionByCode(code) {
  const res = await request.get('/recruiter/position/getUserPublishPositionByCode', { params: { code } });

  return {
    ...res,
    data: adaptPositionList("getUserPublishPositionByCode", [res.data.data] || {})
  };
}

// 个人成功详情接口
export async function getUserPositionById(positionId) {
  const res = await request.get('/recruiter/position/getUserPositionById', { params: { positionId } });
  return {
    ...res,
    data: adaptPositionList("getUserPositionById", [res.data.data] || {})
  };
}

//获取变更岗位详情接口
export async function checkPositionChangeInformation(positionId) {
  const res = await request.get('/recruiter/position/checkPositionChangeInformation', { params: { positionId } })
  return {
    ...res,
    data: adaptPositionList('checkPositionChangeInformation', [res.data.data] || {})
  }
}